package com.leetcode.string.demo6;

/**
 * @Author Zan
 * @Create 2024/6/3 9:34
 * @ClassName: Test
 * @Description : https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/  找出字符串中第一个匹配项的下标
 */
public class Test {
    public static void main(String[] args) {
        String haystack = "abc";
        String needle = "c";
        Solution solution = new Solution();
        int i = solution.strStr(haystack, needle);
        System.out.println(i);
    }
}

class Solution {
    public int strStr(String haystack, String needle) {
        if (haystack.equals(needle)) {
            return 0;
        }
        int start = 0;
        int right = needle.length();
        StringBuffer sb = new StringBuffer(haystack);
        while (right < haystack.length() + 1) {
            if (sb.substring(start, right).equals(needle)) {
                return start;
            }
            start++;
            right++;
        }
        return -1;
    }
}